import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class AutoHideDemo extends JFrame {

    private static final int HIDDEN_WIDTH = 20;  // 隐藏时的宽度
    private static final int TRIGGER_DISTANCE = 10;  // 触发隐藏的距离
    private boolean isHidden = false;
    private int mouseX, mouseY;

    public AutoHideDemo() {
        setTitle("Auto Hide Demo");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setUndecorated(true);  // 去掉默认的边框

        // 添加鼠标监听器，用于拖动窗口
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                mouseX = e.getX();
                mouseY = e.getY();
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                setLocation(e.getXOnScreen() - mouseX, e.getYOnScreen() - mouseY);
                checkAutoHide();
            }
        });

        // 窗口内容
        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setLayout(new BorderLayout());
        layeredPane.add(new JLabel("This is a demo window", SwingConstants.CENTER), BorderLayout.CENTER);
        add(layeredPane);

        // 初始检查隐藏状态
        checkAutoHide();
    }

    private void checkAutoHide() {
        Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();
        Rectangle windowBounds = getBounds();

        if (windowBounds.x <= TRIGGER_DISTANCE && windowBounds.width > HIDDEN_WIDTH) {
            // 停靠在左边并隐藏
            setSize(HIDDEN_WIDTH, windowBounds.height);
            setLocation(0, windowBounds.y);
            isHidden = true;
        } else if (windowBounds.x + windowBounds.width >= screenBounds.width - TRIGGER_DISTANCE && windowBounds.width > HIDDEN_WIDTH) {
            // 停靠在右边并隐藏
            setSize(HIDDEN_WIDTH, windowBounds.height);
            setLocation(screenBounds.width - HIDDEN_WIDTH, windowBounds.y);
            isHidden = true;
        } else if (windowBounds.y <= TRIGGER_DISTANCE && windowBounds.height > HIDDEN_WIDTH) {
            // 停靠在顶部并隐藏
            setSize(windowBounds.width, HIDDEN_WIDTH);
            setLocation(windowBounds.x, 0);
            isHidden = true;
        } else if (windowBounds.y + windowBounds.height >= screenBounds.height - TRIGGER_DISTANCE && windowBounds.height > HIDDEN_WIDTH) {
            // 停靠在底部并隐藏
            setSize(windowBounds.width, HIDDEN_WIDTH);
            setLocation(windowBounds.x, screenBounds.height - HIDDEN_WIDTH);
            isHidden = true;
        } else {
            // 还原窗口
            if (isHidden) {
                setSize(400, 300);
                setLocationRelativeTo(null);
            }
            isHidden = false;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new AutoHideDemo().setVisible(true);
        });
    }
}